/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "i2c.h"
#include "iwdg.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
typedef enum
{
  FALSE = 0,
  TRUE = 1
} bool;
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
volatile uint8_t chuankou[30] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
volatile struct MCU_DATA mcu_data = {0};
volatile struct MCU_KEY mcu_key = {0};
volatile uint8_t MCU_KEY = 0;
volatile uint16_t KEY_ADC[3] = {0};
int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
  return ch;
}

void max7219_send(uint8_t address, uint8_t data)
{
  uint16_t Transmit_buf = (0x00ff & data) | (0xff00 & (address << 8));
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_RESET);
  HAL_SPI_Transmit(&hspi1, (uint8_t *)&Transmit_buf, 1, 1000);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, GPIO_PIN_SET);
}
void max7219_send_1(uint8_t address, uint8_t data)
{
  uint16_t Transmit_buf = (0x00ff & data) | (0xff00 & (address << 8));
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
  HAL_SPI_Transmit(&hspi1, (uint8_t *)&Transmit_buf, 1, 1000);
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
}
void Init_MAX7219(void)
{
  max7219_send(0x09, 0xff);
  max7219_send(0x0a, 0x05);
  max7219_send(0x0b, 0x07);
  max7219_send(0x0c, 0x01);
  max7219_send(0x0f, 0x00);
}
void Init_MAX7219_1(void)
{
  max7219_send_1(0x09, 0xff);
  max7219_send_1(0x0a, 0x05);
  max7219_send_1(0x0b, 0x07);
  max7219_send_1(0x0c, 0x01);
  max7219_send_1(0x0f, 0x00);
}
void KEY_SET(void)
{
  uint8_t setbuf[5] = {0};
  setbuf[0] = 25;
  setbuf[1] = 25;
  if (mcu_key.EVT_MCP_ALTITUDE_SELECTOR != 0)
  {
    setbuf[2] = 1;
    setbuf[3] = mcu_key.EVT_MCP_ALTITUDE_SELECTOR;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_ALTITUDE_SELECTOR = 0;
  }
  if (mcu_key.EVT_MCP_HEADING_SELECTOR != 0)
  {
    setbuf[2] = 2;
    setbuf[3] = mcu_key.EVT_MCP_HEADING_SELECTOR;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_HEADING_SELECTOR = 0;
  }
  if (mcu_key.EVT_MCP_SPEED_SELECTOR != 0)
  {
    setbuf[2] = 3;
    setbuf[3] = mcu_key.EVT_MCP_SPEED_SELECTOR;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_SPEED_SELECTOR = 0;
  }
  /********************************1*************************************/
  if (mcu_key.EVT_MCP_N1 != 0)
  {
    setbuf[2] = 5;
    setbuf[3] = mcu_key.EVT_MCP_N1;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_N1 = 0;
  }
  if (mcu_key.EVT_MCP_SPEED != 0)
  {
    setbuf[2] = 6;
    setbuf[3] = mcu_key.EVT_MCP_SPEED;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_SPEED = 0;
  }
  if (mcu_key.EVT_MCP_VNAV != 0)
  {
    setbuf[2] = 7;
    setbuf[3] = mcu_key.EVT_MCP_VNAV;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_VNAV = 0;
  }
  if (mcu_key.EVT_MCP_LVLCHG != 0)
  {
    setbuf[2] = 8;
    setbuf[3] = mcu_key.EVT_MCP_LVLCHG;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_LVLCHG = 0;
  }
  /********************************1*************************************/
  /********************************2*************************************/
  if (mcu_key.EVT_MCP_HDGSEL != 0)
  {
    setbuf[2] = 9;
    setbuf[3] = mcu_key.EVT_MCP_HDGSEL;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_HDGSEL = 0;
  }
  if (mcu_key.EVT_MCP_LNAV != 0)
  {
    setbuf[2] = 10;
    setbuf[3] = mcu_key.EVT_MCP_LNAV;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_LNAV = 0;
  }
  if (mcu_key.EVT_MCP_VORLOC != 0)
  {
    setbuf[2] = 11;
    setbuf[3] = mcu_key.EVT_MCP_VORLOC;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_VORLOC = 0;
  }
  if (mcu_key.EVT_MCP_APP != 0)
  {
    setbuf[2] = 12;
    setbuf[3] = mcu_key.EVT_MCP_APP;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_APP = 0;
  }
  /********************************2*************************************/
  /********************************3*************************************/
  if (mcu_key.EVT_MCP_ALTHLD != 0)
  {
    setbuf[2] = 13;
    setbuf[3] = mcu_key.EVT_MCP_ALTHLD;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_ALTHLD = 0;
  }
  if (mcu_key.EVT_MCP_CMDA != 0)
  {
    setbuf[2] = 14;
    setbuf[3] = mcu_key.EVT_MCP_CMDA;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_CMDA = 0;
  }
  if (mcu_key.EVT_MCP_CMDB != 0)
  {
    setbuf[2] = 15;
    setbuf[3] = mcu_key.EVT_MCP_CMDB;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_CMDB = 0;
  }
  if (mcu_key.EVT_MCP_DISENGAGE != 0)
  {
    setbuf[2] = 16;
    setbuf[3] = mcu_key.EVT_MCP_DISENGAGE;
    setbuf[4] = 25;
    printf("%s", setbuf);
    mcu_key.EVT_MCP_DISENGAGE = 0;
  }
  /********************************3*************************************/
}
void DATA_SET(void)
{
  if (chuankou[0] == 'h' && chuankou[1] == 'e' && chuankou[2] == 'l' && chuankou[3] == 'l' && chuankou[4] == 'o')
  {
    printf("hello");
  }
  if (chuankou[0] == 99 && chuankou[1] == 99)
  {
    mcu_data.altitude = chuankou[3];
    mcu_data.altitude = (mcu_data.altitude << 8) | chuankou[2];
    max7219_send(5, mcu_data.altitude / 10000);
    max7219_send(6, mcu_data.altitude % 10000 / 1000);
    max7219_send(1, mcu_data.altitude % 1000 / 100);
    max7219_send(2, mcu_data.altitude % 100 / 10);
    max7219_send(3, mcu_data.altitude % 10);
    mcu_data.Heading = chuankou[5];
    mcu_data.Heading = (mcu_data.Heading << 8) | chuankou[4];
    max7219_send_1(1, mcu_data.Heading / 100);
    max7219_send_1(2, mcu_data.Heading % 100 / 10);
    max7219_send_1(3, mcu_data.Heading % 10);
    mcu_data.MCP_IASMach = chuankou[7];
    mcu_data.MCP_IASMach = (mcu_data.MCP_IASMach << 8) | chuankou[6];
    max7219_send_1(4, mcu_data.MCP_IASMach / 100);
    max7219_send_1(5, mcu_data.MCP_IASMach % 100 / 10);
    max7219_send_1(6, mcu_data.MCP_IASMach % 10);
    /********************************1*************************************/
    if (chuankou[8])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
    if (chuankou[9])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
    if (chuankou[10])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
    if (chuankou[11])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
    /********************************1*************************************/
    /********************************2*************************************/
    if (chuankou[12])
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11, GPIO_PIN_SET);
    if (chuankou[13])
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);
    if (chuankou[14])
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
    if (chuankou[15])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
    /********************************2*************************************/
    /********************************3*************************************/
    if (chuankou[16])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
    if (chuankou[17])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
    if (chuankou[18])
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
    if (chuankou[19])
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
    else
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
    /********************************3*************************************/
  }
}
void KEY_GET(void)
{
  // printf("key adc1:%d\r\n", KEY_ADC[0]); // key1:0,key2:2045,key3:2728,key4:3069
  // printf("key adc2:%d\r\n", KEY_ADC[1]); // key1:0,key2:2049,key3:2730,key4:3070
  // printf("key adc3:%d\r\n", KEY_ADC[2]); // key1:0,key2:2045,key3:2730,key4:3070
  /********************************1*************************************/
  if (KEY_ADC[0] < 200)
  {
    while (KEY_ADC[0] < 200)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_N1 = 1;
  }
  if (KEY_ADC[0] < 2100 && KEY_ADC[0] > 2000)
  {
    while (KEY_ADC[0] < 2100 && KEY_ADC[0] > 2000)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_SPEED = 1;
  }
  if (KEY_ADC[0] < 2800 && KEY_ADC[0] > 2600)
  {
    while (KEY_ADC[0] < 2800 && KEY_ADC[0] > 2600)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_VNAV = 1;
  }
  if (KEY_ADC[0] < 3150 && KEY_ADC[0] > 3000)
  {
    while (KEY_ADC[0] < 3150 && KEY_ADC[0] > 3000)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_LVLCHG = 1;
  }
  /********************************1*************************************/
  /********************************2*************************************/
  if (KEY_ADC[1] < 200)
  {
    while (KEY_ADC[1] < 200)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_HDGSEL = 1;
  }
  if (KEY_ADC[1] < 2100 && KEY_ADC[1] > 2000)
  {
    while (KEY_ADC[1] < 2100 && KEY_ADC[1] > 2000)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_LNAV = 1;
  }
  if (KEY_ADC[1] < 2800 && KEY_ADC[1] > 2600)
  {
    while (KEY_ADC[1] < 2800 && KEY_ADC[1] > 2600)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_VORLOC = 1;
  }
  if (KEY_ADC[1] < 3150 && KEY_ADC[1] > 3000)
  {
    while (KEY_ADC[1] < 3150 && KEY_ADC[1] > 3000)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_APP = 1;
  }
  /********************************2*************************************/
  /********************************3*************************************/
  if (KEY_ADC[2] < 200)
  {
    while (KEY_ADC[2] < 200)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_ALTHLD = 1;
  }
  if (KEY_ADC[2] < 2100 && KEY_ADC[2] > 2000)
  {
    while (KEY_ADC[2] < 2100 && KEY_ADC[2] > 2000)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_CMDA = 1;
  }
  if (KEY_ADC[2] < 2800 && KEY_ADC[2] > 2600)
  {
    while (KEY_ADC[2] < 2800 && KEY_ADC[2] > 2600)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_CMDB = 1;
  }
  if (KEY_ADC[2] < 3150 && KEY_ADC[2] > 3000)
  {
    while (KEY_ADC[2] < 3150 && KEY_ADC[2] > 3000)
    {
      // HAL_IWDG_Refresh(&hiwdg);
    }
    mcu_key.EVT_MCP_DISENGAGE = 1;
  }
  /********************************3*************************************/
}
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_ADC1_Init();
  MX_I2C1_Init();
  MX_SPI1_Init();
  MX_USART1_UART_Init();
  MX_TIM1_Init();
  //  MX_IWDG_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  HAL_ADCEx_Calibration_Start(&hadc1);
  HAL_ADC_Start_DMA(&hadc1, (uint32_t *)KEY_ADC, 3);
  HAL_Delay(100);
  MX_IWDG_Init();
  Init_MAX7219();
  Init_MAX7219_1();
  max7219_send(4, 0);
  HAL_UART_Receive_IT(&huart1, (uint8_t *)chuankou, 30);
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    DATA_SET();
    HAL_Delay(5);
    KEY_SET();
    KEY_GET();
    HAL_IWDG_Refresh(&hiwdg);
  }
  /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
   * in the RCC_OscInitTypeDef structure.
   */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI | RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
   */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enables the Clock Security System
   */
  HAL_RCC_EnableCSS();
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
